﻿@using System
@using System.Globalization
@using System.Linq
@using Microsoft.AspNetCore.Diagnostics.Elm
@using Microsoft.AspNetCore.Diagnostics.Elm.Views
@using Microsoft.AspNetCore.DiagnosticsViewPage.Views
@using Microsoft.Extensions.Logging

@functions
{
    public DetailsPage(DetailsPageModel model)
    {
        Model = model;
    }

    public DetailsPageModel Model { get; set; }

    public HelperResult LogRow(LogInfo log)
    {
        return new HelperResult((writer) =>
        {
            if (log.Severity >= Model.Options.MinLevel &&
               (string.IsNullOrEmpty(Model.Options.NamePrefix) || log.Name.StartsWith(Model.Options.NamePrefix, StringComparison.Ordinal)))
            {
                WriteLiteralTo(writer, "        <tr>\r\n            <td>");
                WriteTo(writer, string.Format("{0:MM/dd/yy}", log.Time));
                WriteLiteralTo(writer, "</td>\r\n            <td>");
                WriteTo(writer, string.Format("{0:H:mm:ss}", log.Time));
                var severity = log.Severity.ToString().ToLowerInvariant();
                WriteLiteralTo(writer, $"</td>\r\n            <td class=\"{severity}\">");
                WriteTo(writer, log.Severity);

                WriteLiteralTo(writer, $"</td>\r\n            <td title=\"{log.Name}\">");
                WriteTo(writer, log.Name);

                WriteLiteralTo(writer, $"</td>\r\n            <td title=\"{log.Message}\""+
                    "class=\"logState\" width=\"100px\">");
                WriteTo(writer, log.Message);

                WriteLiteralTo(writer, $"</td>\r\n            <td title=\"{log.Exception}\">");
                WriteTo(writer, log.Exception);

                WriteLiteralTo(writer, "</td>\r\n        </tr>\r\n");
            }
        });
    }

    public HelperResult Traverse(ScopeNode node)
    {
        return new HelperResult((writer) =>
        {
            var messageIndex = 0;
            var childIndex = 0;
            while (messageIndex < node.Messages.Count && childIndex < node.Children.Count)
            {
                if (node.Messages[messageIndex].Time < node.Children[childIndex].StartTime)
                {
                    LogRow(node.Messages[messageIndex]);
                    messageIndex++;
                }
                else
                {
                    Traverse(node.Children[childIndex]);
                    childIndex++;
                }
            }
            if (messageIndex < node.Messages.Count)
            {
                for (var i = messageIndex; i < node.Messages.Count; i++)
                {
                    LogRow(node.Messages[i]);
                }
            }
            else
            {
                for (var i = childIndex; i < node.Children.Count; i++)
                {
                    Traverse(node.Children[i]);
                }
            }
        });
    }
}
@{
    Response.ContentType = "text/html";
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>ASP.NET Core Logs</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.1.min.js"></script>
    <style>
        <%$ include: Shared.css % > <%$ include: DetailsPage.css % >
    </style>
</head>
<body>
    <h1>ASP.NET Core Logs</h1>
    @{
        var context = Model.Activity?.HttpInfo;
    }
    @if (context != null)
    {
        <h2 id="requestHeader">Request Details</h2>
        <table id="requestDetails">
            <colgroup><col id="label" /><col /></colgroup>

            <tr>
                <th>Path</th>
                <td>@context.Path</td>
            </tr>
            <tr>
                <th>Host</th>
                <td>@context.Host</td>
            </tr>
            <tr>
                <th>Content Type</th>
                <td>@context.ContentType</td>
            </tr>
            <tr>
                <th>Method</th>
                <td>@context.Method</td>
            </tr>
            <tr>
                <th>Protocol</th>
                <td>@context.Protocol</td>
            </tr>
            <tr>
                <th>Headers</th>
                <td id="headerTd">
                    <table id="headerTable">
                        <thead>
                            <tr>
                                <th>Variable</th>
                                <th>Value</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var header in context.Headers)
                            {
                            <tr>
                                <td>@header.Key</td>
                                <td>@string.Join(";", header.Value)</td>
                            </tr>
                            }
                        </tbody>
                    </table>
                </td>
            </tr>
            <tr>
                <th>Status Code</th>
                <td>@context.StatusCode</td>
            </tr>
            <tr>
                <th>User</th>
                <td>@context.User.Identity.Name</td>
            </tr>
            <tr>
                <th>Claims</th>
                <td>
                    @if (context.User.Claims.Any())
                    {
                    <table id="claimsTable">
                        <thead>
                            <tr>
                                <th>Issuer</th>
                                <th>Value</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var claim in context.User.Claims)
                                {
                                <tr>
                                    <td>@claim.Issuer</td>
                                    <td>@claim.Value</td>
                                </tr>
                                }
                        </tbody>
                    </table>
                    }
                </td>
            </tr>
            <tr>
                <th>Scheme</th>
                <td>@context.Scheme</td>
            </tr>
            <tr>
                <th>Query</th>
                <td>@context.Query.Value</td>
            </tr>
            <tr>
                <th>Cookies</th>
                <td>
                    @if (context.Cookies.Any())
                    {
                    <table id="cookieTable">
                        <thead>
                            <tr>
                                <th>Variable</th>
                                <th>Value</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var cookie in context.Cookies)
                                {
                                <tr>
                                    <td>@cookie.Key</td>
                                    <td>@string.Join(";", cookie.Value)</td>
                                </tr>
                                }
                        </tbody>
                    </table>
                    }
                </td>
            </tr>
        </table>
    }
    <h2>Logs</h2>
    <form method="get">
        <select name="level">
            @foreach (var severity in Enum.GetValues(typeof(LogLevel)))
            {
                var severityInt = (int)severity;
                if ((int)Model.Options.MinLevel == severityInt)
                {
                    <option value="@severityInt" selected="selected">@severity</option>
                }
                else
                {
                    <option value="@severityInt">@severity</option>
                }
            }
        </select>
        <input type="text" name="name" value="@Model.Options.NamePrefix" />
        <input type="submit" value="filter" />
    </form>
    <table id="logs">
        <thead>
            <tr>
                <th>Date</th>
                <th>Time</th>
                <th>Severity</th>
                <th>Name</th>
                <th>State</th>
                <th>Error</th>
            </tr>
        </thead>
        @Traverse(Model.Activity.Root)
    </table>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#requestHeader").click(function () {
                $("#requestDetails").toggle();
            });
        });
    </script>
</body>
</html>